; 186-byte POSIX sleep(1) utility in Linux x86-64 assembly
; Usage: nasm sleep.s && chmod +x sleep
; Ref: https://redd.it/l4wfoo
; Ref: https://nullprogram.com/blog/2016/11/17/
; This is free and unencumbered software released into the public domain.
bits 64
	db 0x7F, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00
	db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	db 0x02, 0x00, 0x3E, 0x00, 0x01, 0x00, 0x00, 0x00
	db 0x78, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00
	db 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	db 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00
	db 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00
	db 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00
	db 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	db 0x78, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00
	db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	db flen, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	db flen, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
_start: mov edi, [rsp]		; edi = argc
	cmp edi, 2		; argc == 2?
	je .parse
	mov edi, 1		; exit(1)
	jmp .exit
.parse:	mov edx, 10		; assume ecx = eax = 0
	mov rsi, [rsp+16]	; rsi = argv[1]
.loop:	mov cl, [rsi]
	sub ecx, '0'
	jl .sleep
	imul eax, edx
	add eax, ecx
	inc rsi
	jmp .loop
.sleep:	push qword 0		; req.tv_nsec
	push rax		; req.tv_sec
	mov rdi, rsp		; &req
	xor esi, esi		; rem = 0
	mov eax, 35		; SYS_nanosleep
	syscall
	xor edi, edi		; exit(0)
.exit:	mov eax, 60		; SYS_exit
	syscall
flen:	equ $-_start
